package git.soulbgm.common.map;

import git.soulbgm.pojo.SendInfo;
import git.soulbgm.utils.LogUtil;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
import java.util.concurrent.TimeUnit;

/**
 * 发送数据映射
 *
 * @author SoulBGM
 * @date 2024/01/08
 */
public class SendDataMap {

    private static final LogUtil LOG = new LogUtil(SendDataMap.class);
    private static final ExpiringMap<String, SendInfo> SEND_INFO_MAP;

    static {
        SEND_INFO_MAP = ExpiringMap.builder()
                .variableExpiration()
                .maxSize(50000)
                .expirationPolicy(ExpirationPolicy.CREATED)
                .expiration(10, TimeUnit.SECONDS)
                .asyncExpirationListener(SendDataMap::output)
                .build();
    }

    public static void put(SendInfo info) {
        SEND_INFO_MAP.put(info.getPacketId() + "_" + info.getTargetId(), info);
    }

    public static void remove(Long packetId, Integer targetId) {
        SEND_INFO_MAP.remove(packetId + "_" + targetId);
    }

    private static void output(String k, SendInfo v) {
        LOG.info("过期删除, k:{}, 包ID:{}, 数据类型:{}, 包序号:{}", k, v.getPacketId(), v.getDataType(), v.getPacketNumber());
    }

}
